import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../model/PPShopHomePageModel.dart';

class PPRowWrapView extends StatefulWidget {

    PPRowWrapView(List<PPShopPlateContent> data, {Key key, this.rowNum = 4, this.onPressItemAction}):  this.data = data, super(key: key);

    final List<PPShopPlateContent> data;
    final int rowNum;
    final Function onPressItemAction;

    _PPRowWrapViewState createState() => _PPRowWrapViewState();

}

class _PPRowWrapViewState extends State<PPRowWrapView> {
    
    List<PPShopPlateContent> data;
    int rowNum;

    @override
    void initState() {
        data = widget.data == null || widget.data.length == 0 ? [] : widget.data;
        rowNum = widget.rowNum == 0 ? 1 : widget.rowNum;
        super.initState();
    }

    @override
    void didUpdateWidget(PPRowWrapView oldWidget) {
        data = widget.data == null || widget.data.length == 0 ? [] : widget.data;
        rowNum = widget.rowNum == 0 ? 1 : widget.rowNum;
        super.didUpdateWidget(oldWidget);
    }

    @override
    Widget build(BuildContext context) {
        return (Container(
            padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(image: DecorationImage(image: AssetImage('resource/images/wrap_view_background_img.png'), fit: BoxFit.cover)),
            child: Wrap(
                spacing: 0.0,
                direction: Axis.horizontal,
                children: _getRowItems(context)
            ),
        ));
    }

    _getRowItems(BuildContext context) {
        return data.map((item){
            int index = data.indexOf(item);
            return _getRowItemView(context, item, index);
        }).toList();
    }

    Widget _getRowItemView(BuildContext context, PPShopPlateContent data, int index) {
        if(data == null || data.url == null || data.url.length == 0) {
            return null;
        } else {
            double screenWidth = MediaQuery.of(context).size.width;
            double itemWidth = screenWidth / rowNum;
            return (FlatButton(
                padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                child: Container(
                    width: itemWidth,
                    child: Center(
                        child: Column(
                            children: [
                                Image.network(data.url, width: 40, height: 40),
                                Container(
                                    margin: EdgeInsets.only(top: 5),
                                    child: Text(data.contentDesc, maxLines: 1, textAlign: TextAlign.center, style: TextStyle(fontSize: 14, color: Color.fromRGBO(54, 64, 80, 1))),
                                )
                            ],
                        ),
                    )
                ),
                onPressed: (){ _onPressRowItemAction(data, index); },
            ));
        }
    }

    _onPressRowItemAction(PPShopPlateContent data, int index) {
        if(widget.onPressItemAction != null) {
            widget.onPressItemAction(data, index);
        }
    }

}